<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>数据上报查询</title>

<#include "/common/resource.ftl">
    <style type="text/css">
        .set-nav li {
            line-height: 34px;
            height: 34px;
            padding-left: 22px;
            cursor: pointer;
            border-bottom: 1px solid #e6e6e6;
            border-left: 1px solid #e6e6e6;
            border-right: 1px solid #e6e6e6;
            width: 204px;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
        }

        .set-nav li:first-child {
            border-top: 1px solid #e6e6e6;
            border-radius: 2px 2px 0px 0px;
        }

        .set-nav li:last-child {
            border-radius: 0px 0px 2px 2px;
        }

        .set-nav li.active {
            border-color: #3c8dbc;
            background: #3c8dbc;
            color: #fff;
        }
    </style>
    <script type="text/javascript">
        var getSearchParams = function () {
            return {
                taskid: $("#taskid").val() || "",
                templateid: $("#templateid").val() || ""
            };
        }

        function profileSwitch(templateid, obj) {
            $(".set-nav").find("li").removeClass("active");
            $(obj).addClass("active");
            $("#templateid").val(templateid);
            renderDatagrid(templateid);
            getReportTask(templateid);
        }

        // 搜索
        function gotoSearch() {
            if ($("#templateid").val() == "") {
                layer.msg("请选择数据上报模板！");
                return;
            }

            if ($("#taskid").val() == null || $("#taskid").val() == "") {
                //layer.msg("请选择数据上报任务！");
                return;
            }
            $('#datagrid-custom').datagrid({
                url: '${params.contextPath}/web/report/list.json',
                queryParams: getSearchParams()
            });
        }
    </script>
</head>
<body>
<table class="ui-table">
    <tr>
        <td class="ui-table-left" style="width:250px;">
            <!-- ** 左侧栏 ** -->
            <div class="ui-head">
                <span>数据上报模板</span>

            </div>
            <div class="ui-body set-nav">
                <ul style="padding-top1: 20px;" id="templateList">

                </ul>
            </div>
        </td>
        <td class="ui-table-right">
            <div class="ui-operation">
                <div class="ui-buttons">
                    <div class="layui-btn-group tools">
                        <button class="layui-btn layui-btn-normal exportButton"><i class="fa fa-download"></i>导出
                        </button>
                        <button class="layui-btn layui-btn-normal lock-button"><i class="fa fa-stop"></i>锁定</button>
                        <button class="layui-btn layui-btn-normal unlock-button"><i class="fa fa-play"></i>激活</button>
                    </div>
                    <div class="ui-searchs">
                        <div class="value" style="line-height: 30px;">
                            <input type="hidden" name="templateid" id="templateid" value=""/>
                            数据上报任务：<select name="taskid" id="taskid"
                                           style="height: 29px;width:auto;max-width:300px;min-width:100px; "
                                           onchange="gotoSearch()">
                        </select>
                        </div>
                        <input type="button" onclick="gotoSearch()" value="搜索" class="layui-btn layui-btn-danger"
                               style="vertical-align: top"/>
                    </div>
                </div>
            </div>
            <div class="ui-content" style="padding-left: 10px;padding-right:10px;">

            </div>
        </td>
    </tr>
</table>
<table id="datagrid-template" options="url:'${params.contextPath}/web/report/list.json'" style="display: none;">
    <thead>
    <tr>
        <th data-options="field:'ck',checkbox:true"></th>
        <th data-options="field:'stateStr',width:60,formatter:formatState">报送状态</th>
        <th data-options="field:'orgName',width:60">上报单位</th>
        <th data-options="field:'createTimeStr',width:60">上报时间</th>
    </tr>
    </thead>
</table>
<script type="text/javascript">

    function formatState(val, row) {
        if (row.state == 1) {
            return '<span class="ui-stop">' + val + '</span>';
        } else if(row.state == 2){
            return '<span class="ui-accept">' + val + '</span>';
        }else if(row.state == 3){
            return '<span class="ui-warn">' + val + '</span>';
        }
        return val;
    }

    // 获取数据上报模板
    function getReportTemplate() {
        $.ajaxRequest({
            url: '${params.contextPath}/web/reportTemplate/list.json',
            data: {page: 1, rows: 10000},
            success: function (data) {
                var dataList = data.rows;
                var li = null, active = '', template = null, firstTemplate = null;
                for (var i = 0; i < dataList.length; i++) {
                    template = dataList[i];
                    if (template.state != 1) {
                        continue;
                    }
                    if (!firstTemplate) {
                        active = 'class="active"';
                        firstTemplate = template;
                    } else {
                        active = '';
                    }
                    li = $('<li onclick="profileSwitch(\'' +
                            template.id + '\',this)" ' + active + ' title="' + template.name + '">' + template.name + '</li>');
                    $("#templateList").append(li);
                }
                if (firstTemplate) {
                    $("#templateid").val(firstTemplate.id);
                    getReportTask(firstTemplate.id);
                    
                }
            }
        });
    }

    // 根据数据上报模板编号获取数据上报任务
    function getReportTask(templateid) {
        $("#taskid").empty();
        $.ajaxRequest({
            url: '${params.contextPath}/web/task/queryTaskByReportTempalteId.json',
            data: {templateid: templateid},
            success: function (data) {
                var taskList = data.data;
                
                var option = null;
                for (var i = 0; i < taskList.length; i++) {
                    option = $("<option></option>");
                    option.val(taskList[i].id);
                    option.text("【" + (taskList[i].createTimeCNStr) + "】"+taskList[i].title);
                    $("#taskid").append(option);
                }
                renderDatagrid(templateid);
                
            }
        });
    }

    // 根据数据上报模板渲染表格
    function renderDatagrid(templateid) {
        $(".ui-content").empty();// 移除原表格
        var datagrid = $("#datagrid-template").clone();
        datagrid.attr("id", "datagrid-custom").show();
        datagrid.attr("")
        $(".ui-content").append(datagrid);
        // 获取表格显示列
        $.ajaxRequest({
            url: '${params.contextPath}/web/reportTemplate/query.json',
            data: {id: templateid},
            success: function (data) {
                var inputList = data.data.inputList;
                var datagridHeader = $("#datagrid-custom").find('tr:eq(0)');
                if (inputList) {
                    var field = null;
                    for (var i = 0; i < inputList.length; i++) {
                        var input = inputList[i];
                        field = $('<th data-options="field:\'' + input.enName + '\',width:60,formatter:formatValue">' + input.chName + '</th>');
                        datagridHeader.append(field);
                    }
                }
                DataGrid.renderId = "#datagrid-custom";
                DataGrid.init();
                gotoSearch();
            }
        });
    }

    // 格式化模板自定义属性值
    function formatValue(value, row) {
        if (row.data) {
            var val = row.data.json[this.field];
            return val;
        }
        return "";
    }

    $(document).ready(function () {
        getReportTemplate();// 获取数据上报模板

        $(".exportButton").click(function () {
            var templateid = $("#templateid").val();
            var taskid = $("#taskid").val();
            if ($("#templateid").val() == "") {
                layer.msg("请选择数据上报模板！");
                return;
            }

            if ($("#taskid").val() == null || $("#taskid").val() == "") {
                //layer.msg("请选择数据上报任务！");
                return;
            }
            var url = "${params.contextPath}/web/report/export.json?taskid=" + taskid
                    + "&templateid=" + templateid;
            window.location.href = url;
        });
        $(".lock-button").click(function () {
            var templateid = $("#templateid").val();
            var taskid = $("#taskid").val();
            if ($("#templateid").val() == "") {
                layer.msg("请选择数据上报模板！");
                return;
            }

            if ($("#taskid").val() == null || $("#taskid").val() == "") {
                layer.msg("请选择数据上报任务！");
                return;
            }
            var url = "${params.contextPath}/web/taskActor/lock.json?taskId=" + taskid;
            layer.confirm("确定锁定该任务吗?锁定后上报单位将不能继续上报数据", function () {
                $.ajaxRequest({
                    url: url,
                    success: function (resp) {
                        $.message(resp.message);
                        if (resp.success) {
                            DataGrid.reload();
                        }
                    }
                })
            })
        });

   
        $(".unlock-button").click(function () {
            var templateid = $("#templateid").val();
            var taskId = $("#taskid").val();
            if ($("#templateid").val() == "") {
                layer.msg("请选择数据上报模板！");
                return;
            }

            if ($("#taskid").val() == null || $("#taskid").val() == "") {
                layer.msg("请选择数据上报任务！");
                return;
            }

            var rows = DataGrid.getCheckedRows();
            if (!rows||rows.length==0) {
                layer.msg("请选择需要激活的数据！");
                return;
            }
            var orgIds = "";
            $(rows).each(function (i, item) {
                orgIds += item.orgId;
            });
            var url = "${params.contextPath}/web/taskActor/unlock.json";
            layer.confirm("确定激活选中的数据吗?", function () {
                $.ajaxRequest({
                    url: url,
                    data: {
                        taskId: taskId,
                        orgIds: orgIds
                    },
                    success: function (resp) {
                        $.message(resp.message);
                        if (resp.success) {
                            DataGrid.reload();
                        }
                    }
                })
            })
        });
    });
</script>
</body>
</html>
